<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="该题所花费时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="该题所花费时间"></el-input>
    </el-form-item>
    <el-form-item label="得分" prop="getScore">
      <el-input v-model="dataForm.getScore" placeholder="得分"></el-input>
    </el-form-item>
    <el-form-item label="该题分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="该题分数"></el-input>
    </el-form-item>
    <el-form-item label="回答" prop="reply">
      <el-input v-model="dataForm.reply" placeholder="回答"></el-input>
    </el-form-item>
    <el-form-item label="正确答案" prop="correctAnswer">
      <el-input v-model="dataForm.correctAnswer" placeholder="正确答案"></el-input>
    </el-form-item>
    <el-form-item label="问题" prop="question">
      <el-input v-model="dataForm.question" placeholder="问题"></el-input>
    </el-form-item>
    <el-form-item label="试题id" prop="sonId">
      <el-input v-model="dataForm.sonId" placeholder="试题id"></el-input>
    </el-form-item>
    <el-form-item label="试卷id" prop="paperId">
      <el-input v-model="dataForm.paperId" placeholder="试卷id"></el-input>
    </el-form-item>
    <el-form-item label="打分状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="打分状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          time: '',
          getScore: '',
          score: '',
          reply: '',
          correctAnswer: '',
          question: '',
          sonId: '',
          paperId: '',
          status: ''
        },
        dataRule: {
          time: [
            { required: true, message: '该题所花费时间不能为空', trigger: 'blur' }
          ],
          getScore: [
            { required: true, message: '得分不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '该题分数不能为空', trigger: 'blur' }
          ],
          reply: [
            { required: true, message: '回答不能为空', trigger: 'blur' }
          ],
          correctAnswer: [
            { required: true, message: '正确答案不能为空', trigger: 'blur' }
          ],
          question: [
            { required: true, message: '问题不能为空', trigger: 'blur' }
          ],
          sonId: [
            { required: true, message: '试题id不能为空', trigger: 'blur' }
          ],
          paperId: [
            { required: true, message: '试卷id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '打分状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/web/answer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.time = data.answer.time
                this.dataForm.getScore = data.answer.getScore
                this.dataForm.score = data.answer.score
                this.dataForm.reply = data.answer.reply
                this.dataForm.correctAnswer = data.answer.correctAnswer
                this.dataForm.question = data.answer.question
                this.dataForm.sonId = data.answer.sonId
                this.dataForm.paperId = data.answer.paperId
                this.dataForm.status = data.answer.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/web/answer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'time': this.dataForm.time,
                'getScore': this.dataForm.getScore,
                'score': this.dataForm.score,
                'reply': this.dataForm.reply,
                'correctAnswer': this.dataForm.correctAnswer,
                'question': this.dataForm.question,
                'sonId': this.dataForm.sonId,
                'paperId': this.dataForm.paperId,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
